/**
 * Created by liuwd on 14-10-11.
 */

var m_grid={};
m_grid.cols=[
    {field:'code',title:'Code',width:100},
    {field:'name',title:'Name',width:100},
    {field:'price',title:'Price',width:100,align:'right'}
];
m_grid.data=[{name:'jack',sex:'1',age:'1'}];

var curMenu = null, zTree_Menu = null;
var setting = {
    view: {
        showLine: true,
        selectedMulti: false,
        dblClickExpand: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onNodeCreated: this.onNodeCreated,
        beforeClick: this.beforeClick,
        onClick: this.onClick
    }
};

$(function(){
    /**
     * 创建新树
     */
    zTreeNodes = [
        {name:"常用功能",pkid:"101",pid:'',murl:CTX+"/myNote/list"},
        {name:"记事本",pkid:"5",pid:'101',murl:CTX+"/myNote/list"},
        {name:"定制代码",pkid:"6",pid:'101',murl:CTX+"/code/code"},
        {name:"月度下单",pkid:"7",pid:'101',murl:CTX+"/easyOpt/plxd"},
        {name:"明日订单统计",pkid:"8",pid:'101',murl:CTX+"/easyOpt/mrsn"},
        {name:"日送货明细",pkid:"11",pid:'101',murl:CTX+"/easyOpt/day"},
        {name:"月送货明细",pkid:"9",pid:'101',murl:CTX+"/easyOpt/mrsnmx"},
        {name:"月帐",pkid:"12",pid:'101',murl:CTX+"/easyOpt/sumPage"},
        {name:"基础功能",pkid:"0",pid:'',murl:CTX+"/sqInfo/list"},          
        {name:"社区管理",pkid:"1",pid:'0',murl:CTX+"/sqInfo/list"},
        {name:"客户管理",pkid:"2",pid:'0',murl:CTX+"/clientInfo/list"},
        {name:"产品管理",pkid:"3",pid:'0',murl:CTX+"/proInfo/list"},
        {name:"订单管理",pkid:"4",pid:'0',murl:CTX+"/orderInfo/orderList"}
    ];
    initNav({});
    
    //
    $("body .layout-panel-west .panel-header .panel-title").html("");
    var comboxDom = $('<span style="width:140px;">ssss</span>').appendTo("body .layout-panel-west .panel-header .panel-title");
    curSysInfo={sysUrl:"http://www.fit.cn:20003/CPMS"};
    comboxDom.combobox({
    		url: CTX+'/sys.do',
    	    valueField:'sysCode',
    	    textField:'sysName',
    	    onSelect: function(rec){
    	    	initNav({appId:rec.sysCode});
    	    	curSysInfo= rec;
	        }
     });
})
function initNav(params){
	$.utils.ajaxPost(
            CTX+'/menu.do',
            params,
            null,
            function(data){
            	if(data.success){
            		 zTreeNodes = data.data;
            		 $.fn.zTree.init($("#menu_tree"), {
            		        data: {
            		            key: {
            		                name: "menuName"
            		            },
            		            view: {
            		                showLine: true,
            		                showIcon: true,
            		                selectedMulti: false,
            		                dblClickExpand: false

            		            },
            		            simpleData: {
            		                enable: true,
            		                idKey: "id",
            		                pIdKey: "parentId",
            		                rootPId: 0,
            		                iconCls:"iconCls"
            		            }
            		        },
            		        callback: {
            		            onClick: function (event, treeId, treeNode, clickFlag) {
            		                if(treeNode.menuUrl){
            		                    var url =curSysInfo.sysUrl+treeNode.menuUrl;
            		                    addTab(treeNode.menuName,url,true);
            		                }
            		            }
            		        }
            		    }, data.data);
            	}
            }
        )
	
}

function addTab(subtitle, url,closeable){
    if (!$('#main_tab').tabs('exists', subtitle)) {
        $('#main_tab').tabs('addIframeTab',{
            tab:{
                title:subtitle,
                closable:closeable,
                border:false
            },
            iframe:{
                src:url,
                message:"处理中..."
            }
        });
    } else {
        $('#main_tab').tabs('select', subtitle);

        $('#main_tab').tabs('updateIframeTab',{
            which:subtitle,
            iframe:{
                src:url,
                message:"处理中..."
            }
        });
    }
}

var tabTitle="";

function tabMenu(e,title,index){
    tabTitle=title;
    $('#mm').menu('show', {
        left: e.pageX,
        top: e.pageY
    });
    e.preventDefault();
}

function menuHandler(item){
    if(item.name=="closeOther"){
        closeTabs(tabTitle);
    };
    if(item.name=="closeAll"){
        closeTabs("");
    };
}

/**
 * 关闭选项卡，不关闭index的tab。
 * @param index
 */
function closeTabs(title){
    var $tabs =$('#main_tab');
    var selectTabs = $.data($tabs[0],"tabs").selectHis;
    var closeTabs=[];
    for(var i= 1,len=selectTabs.length;i<len;i++){
        var t = selectTabs[i];
        if(t==title){
            continue;
        }
        closeTabs.push(t);
    }
    for(var p in closeTabs){
        $tabs.tabs("close",closeTabs[p]);
    }
}

